using System;
using System.Data;

namespace Asah.SiteWorx.Search
{
	/// <summary>
	/// Defines shared methods to create and add records to dataset
	/// </summary>
	public class PagesDataset
	{
		public PagesDataset()
		{
			//
			// TODO: Add constructor logic here
			//
		}

		/// <summary>
		/// Creates a datset for the pages and returns the result
		/// </summary>
		public static DataSet Create()
		{
			DataSet pgDataSet = new DataSet();
			DataColumn [] keys = new DataColumn[1];
			pgDataSet.Tables.Add(new DataTable("Pages"));

            pgDataSet.Tables["Pages"].Columns.Add("PageId", System.Type.GetType("System.Int32"));
            pgDataSet.Tables["Pages"].Columns.Add("Title", System.Type.GetType("System.String"));
            pgDataSet.Tables["Pages"].Columns.Add("Description", System.Type.GetType("System.String"));
            pgDataSet.Tables["Pages"].Columns.Add("Path", System.Type.GetType("System.String"));
            pgDataSet.Tables["Pages"].Columns.Add("MatchCount", System.Type.GetType("System.Int32"));
            pgDataSet.Tables["Pages"].Columns.Add("Size", System.Type.GetType("System.Decimal"));
            pgDataSet.Tables["Pages"].Columns.Add("Contents", System.Type.GetType("System.String"));
            pgDataSet.Tables["Pages"].Columns.Add("HighlightedContents", System.Type.GetType("System.String"));
            pgDataSet.Tables["Pages"].Columns.Add("Keywords", System.Type.GetType("System.String"));

			pgDataSet.Tables["Pages"].Columns["PageID"].AutoIncrement = true;
            pgDataSet.Tables["Pages"].Columns["PageID"].AutoIncrementSeed = 1;
			keys[0] = pgDataSet.Tables["Pages"].Columns["PageId"];
			pgDataSet.Tables["Pages"].PrimaryKey = keys;
			return pgDataSet;
		}

		/// <summary>
		/// Creates a datset for the pages and returns the result
		/// </summary>
		public static void StoreFile(DataSet dstPgs, Page srchPg)
		{
            //Objects are defined
            DataRow pageRow;

            //New row is created
            pageRow = dstPgs.Tables["Pages"].NewRow();

            //Data is added
            pageRow["Title"] = srchPg.Title;
            pageRow["Description"] = srchPg.Description;
            pageRow["Path"]= srchPg.Path;
            pageRow["MatchCount"] = srchPg.MatchCount;
            pageRow["Size"] = srchPg.Size;
            pageRow["Contents"] = srchPg.Contents;
            pageRow["HighlightedContents"] = srchPg.HighlightedContents;
            pageRow["Keywords"] = srchPg.Keywords;

            //Row is added to the dataset
            dstPgs.Tables["Pages"].Rows.Add(pageRow);		
		}
	}
}
